﻿(function () {
    "use strict";
    var personCollection;

    Vue.component('crud-editform', {
        template: templates["editForm"],
        data: function () {
            return {
                person: {},
                validation: {},
                visible: false
            }
        },
        methods: {
            init(collection) {
                personCollection = collection;
            },
            bind: function (data) {
                this.visible = true;
                this.person = $.extend(true, {}, data);
            },
            showNew: function (data) {
                this.show();
                this.person = {};
            },
            save: function () {
                var isValid = this.validation.validate();
                if (isValid) {
                    if (this.person.id)
                        personCollection.edit(this.person);
                    else
                        personCollection.add(this.person);

                    this.$emit("saveClick", this.person);
                    this.hide();
                }
            },
            hide : function(){
                this.visible = false;
            },
            show : function(){
                this.visible = true;
            },
            cancel: function () {
                this.hide();
                this.$emit("cancelClick");
            }
        }
    });
})();